var temp = new Array();
var shipCounter =0;
function createIds(selector, name) {
	$(selector).find('div').each(function(index) {
		var id = name + index;
		$(this).attr('id', id);
	});
}

function rotateImg(img, divElement) {
	var im = img.attr("src").search(/R/);
	var id = parseInt(divElement.replace("id",""));
	var len = parseInt(img.attr("alt").replace("Ship",""));


	var lastR = id+len-1;
	var lastD = id+(len-1)*10;
	var controlY = true;
	var i=Math.floor(id/10);
	var right = (9 + 10 * i);

	if((lastR)>right){
		controlY = false;
	}

	var controlX = id+10*(len-1);

	var rotUp=true;
	if ($("#id"+lastD).attr("aria-disabled")=="true"){
		rotUp=false;
	}
	var rotDn=true;
	if ($("#id"+lastR).attr("aria-disabled")=="true"){
		rotDn=false;
	}


	if (im == -1 && controlX<100 && rotUp && rotation) {
		var src = img.attr("src").match(/[^\.]+/) + "R.png";
		img.attr("src", src);

	} else {
		if(controlY && rotDn && rotation){
			var src = img.attr("src").replace("R", "");
			img.attr("src", src);
		}
	}

}

function prepareDivs() {
	$('#table').find('div').addClass('dropme');
	createIds('#table', 'id');
	createIds('#table2', 'enemyId');

	$('#table2').find('div').attr("onmouseover", "makeCrosshair(this)");
	$('#table2').find('div').attr("onmouseout", "delCrosshair(this)");

	$('.dragme').dblclick(function() {
		var im = $(this).attr("src").search(/R/);
		if (im == -1) {
			var src = $(this).attr("src").match(/[^\.]+/) + "R.png";
			$(this).attr("src", src);

		} else {

			var src = $(this).attr("src").replace("R", "");
			$(this).attr("src", src);

		}
	});


}
function makeCrosshair(div) {
	console.log(div);
	if($(div).attr("class")=="notClicked"){
		$(div).css({
			"background-color" : "#FFF"
		}); 
	}


}
function delCrosshair(div) {
	console.log(div);
	$(div).css({
		"background-color" : "#60a5bf"
	}); 

}

function makeDraggable() {
	$(".dragme").draggable({
		cursorAt : {
			left : 1,
			top : 1
		},
		cursor : 'none',
		revert : "invalid",
		snap : '.dropme',
		helper : 'clone',
		snapMode : 'inner',
		containment : "#table",

		stop : function(ev, ui) {

			objName = "#clonediv" + counter;
			$(objName).css({
				"left" : 0,
				"top" : 0,
				"position" : "relative"
			});

			// When an existiung object is dragged
			$(objName).draggable({
				cursorAt : {
					left : 1,
					top : 1
				},
				cursor : 'none',
				snap : '.dropme',
				revert : "invalid",
				containment : "#table",
				start : function  (event, ui) {
					temp[0]=$(this).parent().attr("id");
					temp[1]=objName;

					removeShip($(this).parent().attr("id"));
				},
				stop: function(event, ui) {
					if($(this).parent().attr("aria-disabled")=="false"){
						createNewShip($(this), $(this).parent());
					}
				}

			}).dblclick(
					function() {			
						removeShip($(this).parent().attr("id"));
						rotateImg($(this),$(this).parent().attr("id"));
						createNewShip($(this), $(this).parent());
					});
		}

	});
}

function makeDroppable() {
	$(".dropme").droppable({
		tolerance : 'pointer',
		accept : '.dragme',
		drop : function(event, ui) {

			var shipLength = parseInt($(ui.draggable).attr('alt').replace("Ship",""));
			var id = parseInt($(this).attr("id").replace("id",""));
			var lastId = (id+10*(shipLength-1)); 

			if($(ui.draggable).attr("src").search(/R/)==-1){
				lastId = (id+shipLength-1);
			}


			if($("#id"+lastId).attr("aria-disabled")!="true"){

				if ($(ui.draggable).attr('id').match(/sh[1-5]/)) {
					counter++;
					var element = $(ui.draggable).clone();
					$(this).append(element);
					$(ui.draggable).draggable("option", "revert",false);

					element.attr("id", "clonediv"+ counter);

					setShipAmountCounter(ui.draggable.attr('id')+ 'n');
					if (getShipAmountCounter(ui.draggable.attr('id')+ 'n') == 0) {
						shipCounter++;
						$(ui.draggable).draggable("option", "disabled",true);
					}

				} else {


					$(this).append($(ui.draggable));
					$(ui.draggable).css({
						"left" : 0,
						"top" : 0,
						"position" : "relative"

					});
				}
				createNewShip($(ui.draggable), $(this));
				set=true;
			}
			else{
				$(ui.draggable).draggable("option", "revert", true);
				console.log("reverting");

			}

		}
	});
}

function startGame() {
	counter = 0;
	prepareDivs();
	placeEnemyShips();
	makeDraggable();
	makeDroppable();
}

$(document).ready(startGame);

